package com.admin.utils.PayUtil;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * ip tools
 * @author Administrator
 *
 */
public class IpUtil {

	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		/*System.out.println(getIpAddress());
		System.out.println(getMacAddress());
		String url ="http://v.juhe.cn/toutiao/index";
		Map<String,String> map = new HashMap<>();
		map.put("type","top");
		map.put("key","e9e695c900eb553675d8c99de8866f53");
		String result = HttpUtil.doPost(url,map);
		System.out.println(result);*/
		String s = "01040104100410401040104014010401041040";
		for (int i=0;i<s.length();i++){

			System.out.println(s.substring(i,i+2));
		}

	}
	// 获取ip地址
	public static String getIpAddress() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			InetAddress ip = null;
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				// 过滤回环、虚拟、断开的网卡
				if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
					continue;
				}

				// 过滤虚拟机网卡
				if (netInterface.getDisplayName().contains("Virtual")) {
					continue;
				}

				// 过滤VPN
				if (netInterface.getDisplayName().contains("VPN")) {
					continue;
				}

				// 获取真实可用的网卡
				Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()) {
					ip = addresses.nextElement();
					if (ip != null && ip instanceof Inet4Address) { // 必须是IPv4
						return ip.getHostAddress();
					}
				}
			}
		} catch (Exception e) {
			// _logger.error("IP地址获取失败", e);
			e.printStackTrace();
		}
		return "";
	}

	// 获取mac地址
	public static String getMacAddress() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			byte[] mac = null;
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				// 过滤回环、虚拟、断开的网卡
				if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
					continue;
				}

				// 过滤虚拟机网卡
				if (netInterface.getDisplayName().contains("Virtual")) {
					continue;
				}

				// 获取真实可用的网卡
				mac = netInterface.getHardwareAddress();
				if (mac != null) {
					StringBuilder sb = new StringBuilder();
					for (int i = 0; i < mac.length; i++) {
						sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
					}
					if (sb.length() > 0) {
						return sb.toString();
					}
				}
			}
		} catch (Exception e) {
			// _logger.error("MAC地址获取失败", e);
			e.printStackTrace();
		}
		return "";
	}


}
